#pragma once

#include <string>
#include "Reactor.hpp"
#include "InetAddr.hpp"

class Reactor;
class Connection
{
public:
    Connection()
    {
    }
    virtual ~Connection() = default;
    // 回调方法
    virtual void Sender() = 0;
    virtual void Recver() = 0;
    virtual void Excepter() = 0;

    void SetSockfd(int sockfd)
    {
        _sockfd = sockfd;
    }

    void SetEvents(uint32_t events) { _events = events; }
    int GetEvents()
    {
        return _events;
    }

    void SetOwner(Reactor *owner)
    {
        _owner = owner;
    }

    void InTOinbuffer(std::string &buffer) // 写入inbuffer中
    {
        _inbuffer += buffer;
    }

    std::string &Getinbuffer()
    {
        return _inbuffer;
    }

    void InTooutbuffer(std::string &request)
    {
        _outbuffer += request;
    }

    std::string& Getoutbuffer()
    {
        return _outbuffer;
    }

    void DisCardOutString(int n)
    {
        _outbuffer.erase(0, n);
    }

    bool IsOutBufferEmpty() { return _outbuffer.empty(); } 

    Reactor *GetOwner()
    {
        return _owner;
    }

    int Sockfd()
    {
        return _sockfd;
    }

protected:
    int _sockfd;
    std::string _inbuffer;
    std::string _outbuffer;
    InetAddr _peer_addr; // 对应哪一个客户端

    Reactor *_owner; // 回指指针
    int _events;
};